package com.lemon.boot.service.impl;

import com.lemon.boot.common.base.BaseServiceImpl;
import com.lemon.boot.common.consts.ApiConst;
import com.lemon.boot.common.entity.LoginUserDetail;
import com.lemon.boot.common.enums.StatusEnum;
import com.lemon.boot.common.utils.JwtUtil;
import com.lemon.boot.dao.ResourceDAO;
import com.lemon.boot.dto.ResourceDTO;
import com.lemon.boot.po.ResourcePO;
import com.lemon.boot.service.IResourceService;
import com.lemon.boot.vo.RouteResourceVO;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 资源接口方法 业务实现类
 *
 * @author Lemon
 * @since 2024-02-29
 */
@Log4j2
@Service
public class ResourceServiceImpl extends BaseServiceImpl<ResourceDAO, ResourcePO, ResourceDTO> implements IResourceService {
    @Override
    @Transactional
    public String add(ResourcePO resourcePO) {
        return this.addOrEdit(resourcePO);
    }

    @Override
    @Transactional
    public String edit(ResourcePO resourcePO) {
        return this.addOrEdit(resourcePO);
    }

    @Override
    @Transactional
    public String addOrEdit(ResourcePO resourcePO) {
        //1.判断是新增还是修改
        boolean isAdd = resourcePO.getId() == null;

        //2.判断数据是否重复
        boolean exists = this.query().select("1")
                .and(item -> item.eq("name", resourcePO.getName())
                        .or().eq("path", resourcePO.getPath())
                        .or().eq("sign_key", resourcePO.getSignKey()))
                .and(!isAdd, item -> item.ne("id", resourcePO.getId()))
                .last("limit 1").exists();
        if (exists) {
            return "数据重复，请重新输入";
        }

        return super.addOrEdit(resourcePO);
    }

    @Override
    public List<RouteResourceVO> getByCurrent() {
        //1.当前用户
        LoginUserDetail currentUser = JwtUtil.getCurrentUser();

        //2.请求参数
        Map<String, Object> param = ApiConst.enableStatusAndUnableDelete();
        param.put("roleIdList", currentUser.getRoleIdList());
        param.put("tenantId", currentUser.getTenantId());

        //3.查询
        List<RouteResourceVO> routeResourceList = this.baseMapper.getByRoleId(param);
        if (CollectionUtils.isEmpty(routeResourceList)) {
            return routeResourceList;
        }

        //4.处理路由并返回
        return routeResourceList.stream().peek(item -> {
            String path = item.getPath().toLowerCase(Locale.ENGLISH);
            item.setPath(path.endsWith(".vue") ? path.substring(0, path.length() - 4) : path);
        }).collect(Collectors.toList());
    }

    @Override
    public List<ResourcePO> getAllIdNamePath() {
        List<ResourcePO> resourceList = this.query().select("id","name","path").eq(StatusEnum.ENABLE.getColumn(), StatusEnum.ENABLE.getStatus()).list();
        return resourceList.stream()
                .peek(item -> item.setPath(item.getPath().startsWith("/") ? item.getPath() : "/" + item.getPath()))
                .collect(Collectors.toList());
    }
}